// 获取电台接口
import { CHANGE_DIAN_TAI, CHANGE_LOGIN_CODE, CHANGE_LOGIN_STATE } from '../mutaion-types'
// 获取电台的接口测试之后没问题 可用
import { dianTaoi, getLogin } from '@/api'
const home = {
  // 防止重命名和冲突
  namespaced: true,
  state: {
    arr: [],
    // 登陆状态码
    login_code: '403',
    login_state: localStorage.getItem('loginState') === 'ok' || false
  },
  actions: {
    // 电台
    increment ({ commit }) {
      // 异步处理 这里给的电台的接口
      dianTaoi().then(res => {
        console.log('res', res.data)
        // 提交mutations
        commit(CHANGE_DIAN_TAI, res.data.result)
      })
    },
    loginActions ({ commit }, data) {
      return new Promise(resolve => {
        getLogin(data).then(res => {
          commit(CHANGE_LOGIN_CODE, res.data.code)
          if (res.data.code === '200') {
            commit({
              type: CHANGE_LOGIN_STATE,
              payload: true
            })
          }
          resolve(res.data.code)
        })
      })
    }
  },
  mutations: {
    // 改变state唯一值
    [CHANGE_DIAN_TAI] (state, n) {
      console.log('12', state.arr)
      console.log(n)
      state.arr = n
    },
    [CHANGE_LOGIN_CODE] (state, data) {
      state.login_code = data
    },
    [CHANGE_LOGIN_STATE] (state, data) {
      state.login_state = data.payload
    }
  }
}
export default home
